/**
 * File Name	: BaseRowCallbackHandler.java
 * Author		: adelwin
 * Create Date	: 14-Apr-2011:PM 10:20:34
 *
 * Copyright (c) 2011 adelwin. All Rights Reserved. <BR>
 * <BR>
 * This software contains confidential and proprietary information of
 * Adelwin. ("Confidential Information").<BR>
 * <BR>
 * Such Confidential Information shall not be disclosed and it shall
 * only be used in accordance with the terms of the license agreement
 * entered into with Solveware Independent; other than in accordance with the written
 * permission of Solveware Independent. <BR>
 *
 */
package com.uobgroup.cmt.base.handler;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.RowCallbackHandler;

import com.uobgroup.cmt.base.exception.BaseException;
import com.uobgroup.cmt.base.mapper.BaseRowMapper;
import com.uobgroup.cmt.base.model.target.BaseTargetModel;

/**
 * @author adelwin
 *
 */
public abstract class BaseRowCallbackHandler<M extends BaseTargetModel> implements RowCallbackHandler {
	protected Logger logger = Logger.getLogger(BaseRowCallbackHandler.class);

	private BaseRowMapper<M> baseRowMapper;

	public BaseRowMapper<M> getBaseRowMapper() {
		return baseRowMapper;
	}

	public void setBaseRowMapper(BaseRowMapper<M> baseRowMapper) {
		this.baseRowMapper = baseRowMapper;
	}

	public abstract void processRow(M rowItem) throws SQLException, BaseException;
	
	@Override
	public final void processRow(ResultSet resultSet) throws SQLException {
		logger.debug("mapping resultset to row model");
		M rowItem = baseRowMapper.mapRow(resultSet);
		logger.debug("resultset mapped successfully");
		try {
			this.processRow(rowItem);
		} catch (BaseException e) {
			logger.error(e.getMessage(), e);
			// do not throw error
			e.printStackTrace();
		}
	}
}
